package audio;


public class AudioManager {

	private static AudioManager sharedInstance = new AudioManager();

	private SoundFactory factory;
	private Sound currentSound;
	private boolean playingSound = false;

	private AudioManager () {

	}

	public SoundFactory getSoundFactory() {
		return factory;
	}
	
	public void setSoundFactory (SoundFactory factory) {
		this.factory = factory;
	}
	
	public boolean isPlayingSound() {
		return playingSound;
	}

	public synchronized void setPlayingSound(boolean playingSound) {
		this.playingSound = playingSound;
	}

	public void play (Sound sound) {
		if (playingSound == false) {
			currentSound = sound;
			currentSound.playSound();
		}
	}

	public void play (String filePath) throws Exception {
		if (factory == null)
			throw new SoundFactoryException();
		if (playingSound == false) {
			currentSound = factory.createSound(filePath);
			currentSound.playSound();
		}
	}

	public void play (String filePath, Object attributes) throws Exception {
		if (factory == null)
			throw new SoundFactoryException();
		//ATTRIBUTES: filename, fadein, fadeout, starttime, lenght
		if (playingSound == false) {
			currentSound = factory.createSound(filePath, attributes);
			currentSound.playSound();
		}
	}
	
	public float getLenght (String filePath) throws Exception {
		return factory.createSound(filePath).getLenght();
	}

	public void stop () {
		if (currentSound != null)
			currentSound.stopSound();
	}


	public static AudioManager getSharedInstance() {
		return sharedInstance;
	}
	
	public class SoundFactoryException extends Exception {
		public SoundFactoryException () {
			super("Sound Factory not set.");
		}
	}

}
